package com.magina.project.quartz.util;

import lombok.Data;
import org.quartz.CronExpression;

import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class CronUtil {


    public static SimpleDateFormat dateFormat = new SimpleDateFormat();

    public static String getCron(Date  date){
        String dateFormat="ss mm HH dd MM ? yyyy";
        String cronStr = fmtDateToStr(date, dateFormat);
        if (!CronExpression.isValidExpression(cronStr)) {
            throw new RuntimeException( "Illegal cron expression" );   //表达式格式不正确
        }
        return cronStr;
    }

    public static String fmtDateToStr(Date date, String dtFormat) {
        if (date == null)
            return "";
        try {
            dateFormat.applyPattern(dtFormat);
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}
